﻿using System;
using System.Net;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DXTools.Core.CrmSdk;
using DXTools.UI.Core.ViewModel;


namespace DXTools.UI.Modules.ODataQueryDesigner.Model
{
    public enum SortDirection
    {
        asc,
        desc
    }

    public partial class AttributeClass : ViewModelBase
    {

        public AttributeClass(AttributeMetadata Attribute)
        {
            this.Metadata = Attribute;

        }

        public AttributeMetadata Metadata { get; set; }
        public string SchemaName { get { return Metadata.SchemaName; } }
        public AttributeTypeCode? AttributeType { get { return Metadata.AttributeType; } }
        public string EntityLogicalName { get { return Metadata.EntityLogicalName; } }

        public event EventHandler IsAnyUnSelectedEvent;
        public event EventHandler IsSelectAllEvent;
        public event EventHandler IsUnSelectAllEvent;

        private bool _isSelected;

        public string UserLocalizedLabel
        {
            get
            {
                string text = "";
                if ((Metadata.DisplayName != null))
                {
                    text = (Metadata.DisplayName.UserLocalizedLabel != null) ? Metadata.DisplayName.UserLocalizedLabel.Label : Metadata.SchemaName;
                }
                else
                {
                    text = Metadata.SchemaName;
                }

                return text;
            }
        }

        /// <summary>
        /// Allows for the selected option to be set without firing an event. (used for Select All action),
        /// </summary>
        public bool UpdateSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; RaisePropertyChanged("IsSelected"); }
        }

        /// <summary>
        /// Is Selected property that fires events
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                RaisePropertyChanged("IsSelected");
                if (_isSelected == false)
                {
                    if (SchemaName == "(Select All)")
                    {
                        IsUnSelectAllEvent(this, null);
                    }
                    else
                    {
                        IsAnyUnSelectedEvent(this, null);
                    }
                }
                else
                {
                    if (SchemaName == "(Select All)")
                    {
                        IsSelectAllEvent(this, null);
                    }
                }
            }
        }
    }

}

